package p3;

import java.util.ArrayList;

public class PrintArrayListBackwards {

	private static void print( ArrayList<Integer> a ){
		for (int thing : a)
			System.out.print( " " + thing );
	}
	
	private static void print_rec( ArrayList<Integer> a, int n ){
		if (n==0)
			return;
		print_rec( a, n-1 );
		System.out.print( " " + a.get(n-1) );
	}
	
	
	private static void print_rec( ArrayList<Integer> a ){
		print_rec( a, a.size() );
	}

	
	private static void printback( ArrayList<Integer> a ){
		for ( int i=a.size()-1; i>=0; i-- )
			System.out.print( " " + a.get(i) );
	}
	
	private static void printback_rec( ArrayList<Integer> a, int n ){
		if (n==0)
			return;
		System.out.print( " " + a.get(n-1) );
		printback_rec( a, n-1 );
	}
	
	
	private static void printback_rec( ArrayList<Integer> a ){
		printback_rec( a, a.size() );
	}
	
	
	
	public static void main(String[] args) {
		ArrayList<Integer> a = new ArrayList<Integer>();
		final int size = 5;
		
		for (int i=0; i<size; i++)
			a.add( (int) (Math.random() * 10) );

		System.out.print("Iterative forward:  [ ");
		print( a );
		System.out.println(" ]");

		System.out.print("Iterative backward: [ ");
		printback( a );
		System.out.println(" ]");

		System.out.print("Recursive forward:  [ ");
		print_rec( a );
		System.out.println(" ]");

		System.out.print("Recursive backward: [ ");
		printback_rec( a );
		System.out.println(" ]");
	}

}
